<template>
  <div class="userIndex-container">
    <div class="login-box">
      <div class="login-top">
        <i></i>
        <h1>南京消防战备信息管理系统</h1>
      </div>
      <div class="login-form">
        <div class="user">
          <i></i>
          <el-tree-select
            v-model="mechanismId"
            placeholder="请选择机构"
            size="large"
            node-key="xfjyjgTywysbm"
            style="flex: 1"
            :data="treeData"
            :props="props"
            :check-strictly="true"
          />
        </div>
        <div class="password">
          <i></i>
          <el-input
            v-model.trim="pwd"
            type="password"
            placeholder="请输入密码"
            size="large"
            show-password
          />
        </div>
      </div>
      <div class="login-btn" @click="onLogin">
        <span>登&nbsp;&nbsp;&nbsp;录</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { queryFireControlTree, login } from '@/service'
import { ElMessage } from 'element-plus'
import { useLoginInfoStore } from '@/store'
import { useRouter } from 'vue-router'

const store = useLoginInfoStore()
const router = useRouter()

const mechanismId = ref('')
const pwd = ref('')

const props = {
  label: 'jgjc',
  children: 'childXfjgTreeInfo',
  disabled: 'disabled',
}

/** 获取树形数据 */
const treeData = ref([])
queryFireControlTree().then((res: any) => {
  //过滤数据
  deal(res, (node: any) => {
    return node.xfjyjgxzdm.substr(0, 2) !== '09' || node.xfjyjgxzdm === '0911'
  })
  treeData.value = res
})

function deal(nodes: any, predicate: any) {
  // 如果已经没有节点了，结束递归
  if (!(nodes && nodes.length)) {
    return []
  }

  for (const node of nodes) {
    if (predicate(node)) {
      // 如果节点符合条件，禁用
      node.disabled = true
      deal(node.childXfjgTreeInfo, predicate)
    }
  }
}

/** 登录 */
const onLogin = () => {
  if (!mechanismId.value) {
    return ElMessage.warning('请选择机构')
  }
  if (!pwd.value) {
    return ElMessage.warning('请输入密码')
  }

  login({ jgId: mechanismId.value, password: pwd.value })
    .then((res: any) => {
      // 登录信息存储
      sessionStorage.setItem('loginInfo', JSON.stringify({ ...res, jgId: mechanismId.value }))
      store.loginInfo = { ...res, jgId: mechanismId }
      // 路由跳转
      router.push({ path: '/jgPersonnelInfo' })
      ElMessage.success('登录成功')
    })
    .catch(() => {
      ElMessage.error('密码错误')
    })
}
</script>

<style lang="scss" scoped>
.userIndex-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-image: url('../../assets/image/login_bck.png');
  background-size: 101% 101%;
  .login-box {
    width: 470px;
    height: 350px;

    .login-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      i {
        width: 50px;
        height: 50px;
        background-image: url('../../assets/image/login-icon.png');
        background-size: 100% 100%;
      }
      h1 {
        text-align: center;
        font-size: 34px;
        font-weight: 700;
        color: #fff;
      }
    }

    .login-form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      width: 400px;
      height: 200px;
      background-color: #fff;
      border-radius: 15px;
      margin: 15px auto 0;
      .user {
        display: flex;
        align-items: center;
        margin: -15px 10px 20px;
        padding: 0 15px 5px;
        border-bottom: 1px solid #e5e8eb;
        i {
          width: 22px;
          height: 22px;
          background-image: url('../../assets/image/user-icon.png');
          background-size: 110% 100%;
        }
      }
      .password {
        display: flex;
        align-items: center;
        margin: 0 10px;
        padding: 0 15px 5px;
        border-bottom: 1px solid #e5e8eb;
        i {
          width: 22px;
          height: 22px;
          background-image: url('../../assets/image/password-icon.png');
          background-size: 110% 100%;
        }
      }

      .user,
      .password {
        :deep(.el-input__wrapper) {
          box-shadow: 0 0 0 0px var(--el-input-border-color, var(--el-border-color)) inset !important;
          cursor: default;
        }
      }
    }

    .login-btn {
      width: 280px;
      height: 50px;
      line-height: 50px;
      margin: 30px auto;
      border-radius: 50px;
      color: #fff;
      font-size: 20px;
      font-weight: 700;
      text-align: center;
      background-color: rgba(0, 0, 0, 0.4);
      cursor: pointer;
    }
  }
}
</style>
